package com.zhm.demo.cloud;

import com.alibaba.nacos.common.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.List;

@Slf4j
@Component
public class TestCloud {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private IotDebugConfig iotDebugConfig;
//
//    @Autowired
//    private ConfigService configService;


    //    @PostConstruct
    public void init() {
        new Thread(() -> {
            while (true) {
                System.out.println(iotDebugConfig);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                List<String> services = discoveryClient.getServices();
                log.info("Services: {}", services);
                if (CollectionUtils.isNotEmpty(services)) {
                    for (String service : services) {
                        List<ServiceInstance> instances = discoveryClient.getInstances(service);
                        log.info("********{}-{}", service, instances);
                        instances.stream().forEach(instance -> {
                            URI uri = instance.getUri();
                            log.info("{}", uri.toString());
                        });
                    }
                }
            }
        }).start();
    }
}
